<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/2/19
 * Time: 10:00
 */

namespace app\admin\controller;


use app\admin\model\NewsCate as NewsCateModel;
use think\facade\Request;

class NewsCate extends Common
{
    /**
     * 新闻栏目列表
     * @return array|\think\response\View
     * @throws \think\exception\DbException
     */
    public function newsCateList()
    {
        if (Request::isAjax()) {
            $cate_name = trim(input("cate_name"));
            $cate_id = trim(input("cate_id"))?trim(input("cate_id")):0;

            // where条件
            $where = [];
            $where[] = ["cate_name","like",'%'.$cate_name.'%'];
            $where[] = ["is_del","=",'1'];

            if($cate_id != 0)
            {
                $where[] = ['cate_id','=',$cate_id];
            }

            //获取分页参数
            $pageOption = $this->getPageAndPageSize(input('page'),input('limit'));

            //获取列表
            $list = NewsCateModel::getPageListByPage($pageOption['page'],$pageOption['pageSize'],$where,['cate_id'=>'desc','sort'=>'asc'],'*');
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return view("news_cate_list");
    }

    /**
     * 排序
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function newsCateSort()
    {
        if (Request::isAjax()) {
            $id = trim(input("id"));
            $sort = trim(input("sort"));

            //更新sort
            $r = NewsCateModel::updateOneData(['id'=>$id],["sort"=>$sort]);
            if($r !== false)
            {
                return $result = ["code"=>0,"msg"=>"更新成功","data"=>[]];
            }
            else{
                return $result = ["code"=>1,"msg"=>"更新失败","data"=>[]];
            }
        }
    }

    /**
     * 新闻删除
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function newsCateDel()
    {
        if (Request::isAjax()) {
            $id = trim(input("id"));

            //更新is_del
            $r = NewsCateModel::softDel($id);
            if($r !== false)
            {
                return $result = ["code"=>0,"msg"=>"删除成功","data"=>[]];
            }
            else{
                return $result = ["code"=>1,"msg"=>"删除失败","data"=>[]];
            }
        }
    }

    /**
     * 添加新闻栏目
     * @return array|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function newsCateAdd()
    {
        if (Request::isAjax()) {
            $data['cate_name'] = trim(input("cate_name"));
            $data['sort'] = trim(input("sort"));
            $data['cate_id'] = trim(input("cate_id"));

            //验证
            $where[] = ["cate_name","=",$data['cate_name']];
            $cate_name = NewsCateModel::getFieldValue("id,cate_name",$where);
            if(!empty($cate_name['cate_name']))
            {
                return $result = ["code"=>1,"msg"=>"栏目已存在，请重新输入！","data"=>[]];
            }

            //添加
            $r = NewsCateModel::create($data);
            if($r !== false)
            {
                return $result = ["code"=>0,"msg"=>"插入成功","data"=>[]];
            }
            else{
                return $result = ["code"=>1,"msg"=>"插入失败","data"=>[]];
            }
        }
        return view("news_cate_add");
    }

    /**
     * 编辑新闻栏目
     * @return array|\think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function newsCateEdit()
    {
        if (Request::isAjax()) {
            $data['id'] = trim(input("id"));
            $data['cate_name'] = trim(input("cate_name"));
            $data['cate_id'] = trim(input("cate_id"));
            $data['sort'] = trim(input("sort"));

            //验证
            $where[] = ["cate_name","=",$data['cate_name']];
            $where[] = ["id","<>",$data['id']];
            $cate_name = NewsCateModel::getFieldValue("id,cate_name",$where);
            if(!empty($cate_name['cate_name']))
            {
                return $result = ["code"=>1,"msg"=>"与其他栏目重名，请重新输入！","data"=>[]];
            }

            //修改
            $where = [];
            $where[] = ["id","=",$data['id']];
            $updateData = [
                "cate_name"=>$data['cate_name'],
                'sort'=>$data['sort'],
                'cate_id'=>$data['cate_id']
            ];
            $r = NewsCateModel::updateOneData($where,$updateData);
            if($r !== false)
            {
                return $result = ["code"=>0,"msg"=>"修改成功","data"=>[]];
            }
            else{
                return $result = ["code"=>1,"msg"=>"修改失败","data"=>[]];
            }
        }
        $id = trim(input("id"));
        $where[] = ["id","=",$id];
        $info = NewsCateModel::getFieldValue('*',$where);
        $this->assign("info",$info);
        return view("news_cate_edit");
    }

}